package com.dianjing.dianqun.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dianjing.dianqun.common.ResponseData;
import com.dianjing.dianqun.common.base.BaseRestController;
import com.dianjing.dianqun.model.FriendsRelation;
import com.dianjing.dianqun.model.Site;
import com.dianjing.dianqun.model.User;
import com.dianjing.dianqun.service.FriendsRelationService;
import com.dianjing.dianqun.service.SiteService;
import com.dianjing.dianqun.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @date : 2017年10月13日
 * 
 * @author: baoyongtao
 * 
 * @see :
 * 
 */
@Api(value = "SiteApiController", description = "站点相关api")
@RestController
@RequestMapping("/api/site")
public class SiteApiController extends BaseRestController {
	//默认
	public static final String SITE_SORT_TYPE_DEFAULT="site_sort_type_default:";
	//浏览量
	public static final String SITE_SORT_TYPE_VIEWS="site_sort_type_views:";
	//关注量
	public static final String SITE_SORT_TYPE_FOLLOW="site_sort_type_follow:";
	

	@Autowired
	private SiteService siteService;

	@Autowired
	private UserService userService;
	
	@Autowired
	private FriendsRelationService relationService;
	
	@GetMapping("/{id}")
	@ApiOperation(value = "查询站点信息", notes = "根据站点id查询站点信息和访问量")
	@ApiImplicitParam(name = "id", value = "站点id", dataType = "int", required = true, paramType = "path")
	public ResponseData get(@PathVariable(required = true) Integer id) {
		Site selectByPrimaryKey = siteService.selectByPrimaryKey(id);
		if (null != selectByPrimaryKey) {
			Long count = siteService.selectFlowCountBySiteId(selectByPrimaryKey.getSiteId());
			selectByPrimaryKey.setTotalCount(count);
		}
		return new ResponseData(ResponseData.STATUS_OK, "请求成功", selectByPrimaryKey);
	}

	@GetMapping("/query")
	@ApiOperation(value = "查询站点信息", notes = "分页查询站点信息和访问量")
	@ApiImplicitParams({ /*@ApiImplicitParam(dataType = "string", name = "city", value = "城市地区", paramType = "query"),*/
			@ApiImplicitParam(dataType = "int", name = "userId", required = false, value = "当前用户Id", paramType = "query"),
			@ApiImplicitParam(dataType = "int", name = "sort", value = "排序方式（参数说明  1：默认 2：浏览量 3：关注度）", paramType = "query"),
			@ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页多少记录", paramType = "query"),
			@ApiImplicitParam(dataType = "int", name = "pageNum", value = "当前页", paramType = "query") })
	public ResponseData get(@RequestParam(required = false) Integer userId,
			/*@RequestParam(defaultValue = "0") Integer city,*/ @RequestParam(defaultValue = "1") Integer sort, 
			@RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageNum) {
		PageHelper.startPage(pageNum, pageSize);
		List<Site> list = null;
		//针对综合排序，浏览量排序，关注量排序
		if(sort>=1 && sort<=3) {
			if(sort==1) {//默认排序
				list = siteService.findAll();
			}else if(sort==2) {//浏览量查询
				 list = siteService.findSiteByView();
			}else if(sort==3) {//关注度查询
				list =  siteService.findSiteByFollows();
				for (Site site : list) {
						//统一处理浏览数量 
					Long count = siteService.selectFlowCountBySiteId(site.getSiteId());
					site.setTotalCount(count);
				}
			}
		}
		if(userId!=null) {
			for (Site site : list) {
				User user = userService.selectUserBySiteId(site.getSiteId());
				// 查询我的粉丝 也就是 不是不当前用户是我的粉丝 来判断关注 状态
				FriendsRelation relation = relationService.selectMyFlowUser(user.getUserId(), userId);
				site.setIsFlow(relation!=null?true:false);
			}
		}
		PageInfo<Site> pageInfo = new PageInfo<Site>(list);
		return new ResponseData(ResponseData.STATUS_OK, "请求成功", pageInfo);
	}
	
}
